﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Data.GameData.Interfaces;

namespace Data.GameData
{
    public class Fleet : MapObject, IFleetElement
    {
        private string name;
        private Dictionary<Guid, IFleetElement> roster = new Dictionary<Guid, IFleetElement>();

        private Order orders = null;
        private double bestSpeed = 0;
        private int turnMode = 2;

        #region Constructor
        public Fleet()
        {
            ImageName = @"White Counters\19 FLEET.gif";
        }
        #endregion

        #region Properties
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        #endregion

        #region Fleet Managemenmt
        public IEnumerable<IFleetElement> Roster
        {
            get { return roster.Values; }
        }

        public void AddFleetUnit(IFleetElement unit)
        {
            if (unit is Fleet) AddFleetUnits(unit as Fleet);

            if (!roster.ContainsKey(unit.Id))
            {
                roster.Add(unit.Id, unit);

                BestSpeed = roster.Min(u => u.Value.BestSpeed);
                TurnMode = roster.Max(u => u.Value.BestSpeed);
            }
        }

        public void AddFleetUnits(IEnumerable<IFleetElement> units)
        {
            foreach (IFleetElement unit in units)
                AddFleetUnit(unit);
        }

        public void AddFleetUnits(Fleet fleet)
        {
            AddFleetUnits(fleet.Roster);
        }

        public void RemoveFleetUnits(IEnumerable<IFleetElement> units)
        {
            foreach (Unit unit in units)
            {
                roster.Remove(unit.Id);

                BestSpeed = roster.Min(u => u.Value.BestSpeed);
                TurnMode = roster.Max(u => u.Value.BestSpeed);
            }
        }
        #endregion

        #region IFleetElement Methods
        public bool HasOrders
        {
            get { return orders != null; }
        }

        public Order Orders
        {
            get { return orders; }
            set { orders = value; }
        }

        public int BestSpeed
        {
            get { return (int)bestSpeed; }
            protected set { bestSpeed = value; }
        }

        public int TurnMode
        {
            get { return turnMode; }
            protected set { turnMode = value; }
        }
        #endregion
    }
}
